package apis

import (
	//"iotms/constant"

	"github.com/gin-gonic/gin"
	"common/logger"
	"github.com/jinzhu/gorm"
	"tylnmall/models"
	"net/http"
	"tylnmall/constant"
	"github.com/juju/errgo/errors"
	"fmt"
)

func LoginApi(c *gin.Context) {
	type param struct {
		UserName string `json:"username" form:"username"`
		PassWord string `json:"password" form:"password"`
	}
	var p param
	var err error

	db := c.MustGet("ty").(*gorm.DB)


	if err = c.Bind(&p); err != nil {
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorParamWrong, "err_msg": constant.TranslateErrCode(constant.ErrorParamWrong)})
		return
	}

	if p.UserName == "" && p.PassWord == "" {
		err = errors.New("username/password is missing")
		logger.Error(err)
		c.AbortWithStatus(http.StatusBadRequest)
		return
	}

	var user []*models.User
	if err = db.Where("username=?",p.UserName).Find(&user).Error; err != nil || len(user)==0{
		err = errors.New("admin not found")
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.AdminNotExists, "err_msg": constant.TranslateErrCode(constant.AdminNotExists)})
		return
	}
	fmt.Println(user[0].Password)
	fmt.Println(p.PassWord)
	if user[0].Password != p.PassWord {
		err = errors.New("incorrect password")
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.WrongUsernamePassword, "err_msg": constant.TranslateErrCode(constant.WrongUsernamePassword)})
		return
	}
	c.JSON(http.StatusOK, gin.H{"err_code": constant.Success, "data": user[0]})
}

func LoginForAdminApi(c *gin.Context){
	type param struct {
		UserName string `json:"username" form:"username"`
		PassWord string `json:"password" form:"password"`
	}
	var p param
	var err error

	db := c.MustGet("ty").(*gorm.DB)


	if err = c.Bind(&p); err != nil {
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorParamWrong, "err_msg": constant.TranslateErrCode(constant.ErrorParamWrong)})
		return
	}

	if p.UserName == "" && p.PassWord == "" {
		err = errors.New("username/password is missing")
		logger.Error(err)
		c.AbortWithStatus(http.StatusBadRequest)
		return
	}

	var user []*models.User
	if err = db.Where("username = ? and type = 0",p.UserName).Find(&user).Error; err != nil || len(user)==0{
		err = errors.New("admin not found")
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.AdminNotExists, "err_msg": constant.TranslateErrCode(constant.AdminNotExists)})
		return
	}
	fmt.Println(user[0].Password)
	fmt.Println(p.PassWord)
	if user[0].Password != p.PassWord {
		err = errors.New("incorrect password")
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.WrongUsernamePassword, "err_msg": constant.TranslateErrCode(constant.WrongUsernamePassword)})
		return
	}
	c.JSON(http.StatusOK, gin.H{"err_code": constant.Success, "data": user[0]})
}

func RegisteredApi(c *gin.Context)  {
	var user models.User
	var err error
	if err = c.Bind(&user); err != nil {
		logger.Error(err)
		return
	}
	if user.Username==""||user.Password==""||user.Name==""||user.Phone==""{
		err = errors.New("incorrect val")
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorParamNotEnough, "err_msg": constant.TranslateErrCode(constant.ErrorParamNotEnough)})
		return
	}
	if len([]rune(user.Password))>20||len([]rune(user.Password))<4{
		err = errors.New("incorrect val")
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorLength, "err_msg": constant.TranslateErrCode(constant.ErrorLength)})
		return
	}
	db := c.MustGet("ty").(*gorm.DB)
	var Users []*models.User
	if err = db.Find(&Users).Error; err != nil {
		err = errors.New("admin not found")
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.AdminNotExists, "err_msg": constant.TranslateErrCode(constant.AdminNotExists)})
		return
	}
	for _,u := range Users{
		if user.Username == u.Username{
			err = errors.New("username is used")
			logger.Error(err)
			c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorUserName, "err_msg": constant.TranslateErrCode(constant.ErrorUserName)})
			return
		}
	}
	if err = db.Create(&user).Error;err!=nil{
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorUnknown, "err_msg": constant.TranslateErrCode(constant.ErrorUnknown)})
		return
	}
	c.JSON(http.StatusOK, gin.H{"user":user})
}
